<%@ page import="java.net.URLEncoder" %>
<%@ page language="java" pageEncoding="UTF-8" errorPage="login.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%
	request.setCharacterEncoding("UTF-8");

	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;

	String username = "";
	int visitTimes 	= 0;

	Cookie[] cookies = request.getCookies();
	for(int i=0; cookies!=null&&i<cookies.length; i++) {
		Cookie cookie = cookies[i];

		//登录
		if ("username".equals(cookie.getName())) {
			username = URLEncoder.encode(cookie.getValue(), "UTF-8");   //URLEncoder 解码
		} else if ("visitTimes".equals(cookie.getName())) {
			visitTimes = Integer.parseInt(cookie.getValue());
			cookie.setValue("" + ++visitTimes);
		}
	}
	
	// 如果没有找到 Cookie 中保存的用户名，则转到登录界面
	if(username == null || username.trim().equals("")){
		throw new Exception("您还没有登录。请先登录");
	}

	// 修改 Cookie，更新用户的访问次数
	Cookie visitTimesCookie = new Cookie("visitTimes", Integer.toString(++visitTimes));
	response.addCookie(visitTimesCookie);

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Cookie</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" charset="utf-8">
	<jsp:include page="/jquery/jquery-1.11.0.min.js" flush="true" />
</script>
<script type="text/javascript" charset="utf-8">
	<jsp:include page="/jquery/plugins/jquery.cookie.js" flush="true" />
</script>
<%--尝试了下面三种方式都不行，只有上面那种，预先将js内容放入script标签才可以。注意：这个页面是 sendRedirect 重定向(302)过来的。顾海彬他们公司用的 c:url 却可以，疑惑中！--%>
<%--<script type="text/javascript" src="/jquery/jquery-1.11.0.min.js" />--%>
<%--<script type="text/javascript" charset="utf-8" src="<c:url value='/jquery/jquery-1.11.0.min.js' />" />--%>
<%--<script type="text/javascript" charset="utf-8" src="<c:url value='/jquery/plugins/jquery.cookie.js' />" />--%>
</head>
<body>
<div align="center" style="margin:10px; ">
	<fieldset>
		<legend>登录信息</legend>
		<form action="login.jsp" method="post">
			<table>
				<tr>
					<td>
						您的帐号：
					</td>
					<td>
						<%= username %>
					</td>
				</tr>
				<tr>
					<td>
						登录次数：
					</td>
					<td>
						<%= visitTimes %>
					</td>
				</tr>
				<tr>
					<td>
					</td>
					<td>
						<input type="button" value=" 刷  新 " 
							onclick="location='<%= request.getRequestURI() %>?ts=' + new Date().getTime(); "
							class="button">
					</td>
				</tr>
			</table>
		</form>
	</fieldset>
	<br />
	<!--Cookie最大值和最多数量的限制大多数浏览器支持最大为 4096 字节的 Cookie-->
	从 Cookie 中获取到的二进制图片：<img src="/session/base64_decode.jsp" /> <br/>
	<textarea id='cookieArea' style='width:100%; height:200px; '></textarea>
</div>
<script type="text/javascript">
	$(document).ready(function() {
		var content = $.cookie("file");
		$("#cookieArea").val(content);
	});
</script>
</body>
</html>
